package com.woniuxy.lrz.carlproject.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.util.Results;
import com.woniuxy.lrz.carlproject.controller.form.formcar.CarAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.controller.form.formcar.CarDetailsAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.controller.form.shopform.formcar.CarDetailsFindForm;
import com.woniuxy.lrz.carlproject.controller.form.formcar.CarTypeAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.model.CarDetail;
import com.woniuxy.lrz.carlproject.service.CarDetailService;
import com.woniuxy.lrz.carlproject.service.dto.dtocar.CarDetailsDto;
import com.woniuxy.lrz.carlproject.service.dto.dtocar.CarDetailsTypeBrandDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车型详情表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@Api(tags = "车辆具体型号接口")
@Slf4j
@Validated
    @RequestMapping("/carlproject/carDetail")
public class CarDetailController {
    @Resource
    private CarDetailService carDetailService;


    @PostMapping("/addOrUpdateCarDetail")
    @ApiOperation("车辆具体信息新增或修改")
    public Results addOrUpdateCarDetail(@RequestBody @Valid CarDetailsAddOrUpdateForm carDetailsAddOrUpdateForm){
        carDetailService.addOrUpdateCarDetail(carDetailsAddOrUpdateForm);
        return Results.instance().setMessage("操作成功");
    }

    @DeleteMapping("/deleteCarDetails")
    @ApiOperation("删除车辆具体信息")
    @ApiImplicitParam(name = "carDetailsId",value = "车辆具体信息Id")
    public Results deleteCarDetails(@NotNull(message = "车辆具体信息Id不能为空") Integer carDetailsId){
        carDetailService.deleteCarDetails(carDetailsId);
        return Results.instance().setMessage("删除成功");
    }

    @GetMapping("/findCarDetailsCondition")
    @ApiOperation("加载型号具体信息下拉列表，并存入redis中")
    @ApiImplicitParam(name = "carTypeId",value = "车型Id")
    @Cacheable(value = "car",key = "'findCarDetails:'+#carTypeId")
    public Results findCarDetailsCondition(@NotNull(message = "车型Id不能为空") Integer carTypeId){
        List<CarDetail> car_type_id = carDetailService.list(new QueryWrapper<CarDetail>().eq("car_type_id", carTypeId));
        List<CarDetailsDto> carDetailsDtos = BeanUtil.copyToList(car_type_id, CarDetailsDto.class);
        return Results.instance().setData(carDetailsDtos).setMessage("加载型号具体信息下拉列表成功");
    }

    @GetMapping("/findCarDetails")
    @ApiOperation("查询车辆具体型号信息")
    public Results findCarDetails(@Valid CarDetailsFindForm carDetailsFindForm){
        Page<CarDetailsTypeBrandDto> carDetails = carDetailService.findCarDetails(carDetailsFindForm);
        return Results.instance().setMessage("查询车辆具体型号信息成功").setData(carDetails);
    }





}
